package com.example.chatweb.controller;

import com.example.chatweb.entity.History;
import com.example.chatweb.service.ChatService;
import com.example.chatweb.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//import static jdk.nashorn.internal.objects.Global.print;

@RestController
public class HistoryController {
    private final HistoryService historyService;

   @Autowired
   public HistoryController(HistoryService historyService) {
          this.historyService = historyService;
   }


   @PostMapping("/insert_history")
   public void insert(@RequestBody History history){
       historyService.addHistory(history);
   }

    @GetMapping("/by-topic")
    public ResponseEntity<List<History>> findHistoriesByTopic(@RequestParam("topic") String topic,@RequestParam("userid") Integer userid) {
        List<History> histories = historyService.findHistoriesByTopic(topic,userid);
        if (histories.isEmpty()) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(histories);
    }

    @GetMapping("/all_topic")
    public ResponseEntity<List<String>> findAllTopics(@RequestParam("userid") Integer userid) {
           List<String> topics = historyService.findAllTopics(userid);
           if(topics.isEmpty()) {
               return ResponseEntity.notFound().build();
           }
           return ResponseEntity.ok(topics);
    }



}
